package net.haroeris.wh40kcampaign.beans.to;

import java.util.Collection;
import java.util.LinkedList;

import net.haroeris.wh40kcampaign.beans.po.Army;
import net.haroeris.wh40kcampaign.beans.po.Player;
import net.haroeris.wh40kcampaign.dao.Authorities;

public class PlayerTO {
    private String name;
    private Collection<Authorities> authorities;
    private Collection<Army> armies;


    public PlayerTO(Player playerPo) {
        setName(playerPo.getUsername());

        Collection<Authorities> newAuthorities= new LinkedList<Authorities>();
        if( playerPo.getAuthorities()!=null ){
            for( String authString : playerPo.getAuthorities() ){
                Authorities thisAuth= Authorities.getByAuthorities(authString);
                if( thisAuth!=null ){
                    newAuthorities.add(thisAuth);
                }
            }
        }
        setAuthorities(newAuthorities);
    }

    public PlayerTO() {
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the authorities
     */
    public Collection<Authorities> getAuthorities() {
        return authorities;
    }
    /**
     * @param authorities the authorities to set
     */
    public void setAuthorities(Collection<Authorities> authorities) {
        this.authorities = authorities;
    }
    /**
     * @return the armies
     */
    public Collection<Army> getArmies() {
        return armies;
    }
    /**
     * @param armies the armies to set
     */
    public void setArmies(Collection<Army> armies) {
        this.armies = armies;
    }

    @Override
    public String toString(){
        StringBuffer buffer= new StringBuffer();
        buffer.append("[").append(getClass().getSimpleName()).append(" name=").append(getName());

        if( getAuthorities()!=null ){
            buffer.append("; authorities=");
            boolean first= true;
            buffer.append("(");
            for( Authorities authority : getAuthorities() ){
                if( !first ){
                    buffer.append(", ");
                }
                buffer.append( authority );
            }
            buffer.append(")");
        }
        buffer.append("]");

        return buffer.toString();
    }
}